package com.leyou.user.controller;

import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.UserContext;
import com.leyou.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/16 14:06
 * @description：
 */
@RestController
@RequestMapping("/info")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户名和密码查询
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping
    public ResponseEntity<UserDTO> queryUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password) {

        return ResponseEntity.ok(userService.queryUsernameAndPassword(username, password));
    }

    /**
     * 注册
     *
     * @param user
     * @param code
     * @return
     */
    @PostMapping
    public ResponseEntity register(@Valid User user, @RequestParam("code") String code) {
        userService.register(user, code);
        return ResponseEntity.status(200).build();
    }

    /**
     * 发送短信验证码
     *
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity sendMessages(@RequestParam("phone") String phone) {
        userService.sendMessages(phone);
        return ResponseEntity.status(200).build();
    }

    /**
     * 判断信息是否存在
     *
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> checkUsernameOrPhone(@PathVariable("data") String data, @PathVariable("type") Integer type) {
        return ResponseEntity.ok(userService.checkUsernameOrPhone(data, type));
    }

    @GetMapping("/me")
        public ResponseEntity<UserDetail> checkLogin(@CookieValue("LY_TOKEN") String token) {
//        return ResponseEntity.ok(userService.checkLogin(token));
        return ResponseEntity.ok(UserContext.getUser());
    }

}
